/*
 * Created on 2005-7-11
 */
/**
 * 
 */
package com.linkage.lcims.base.dao.broadband.impl;

import java.util.List;
import java.util.Map;

import com.linkage.lcims.base.dao.HibernateBaseDAOImpl;
import com.linkage.lcims.base.dao.broadband.interfaces.BroadbandUserDAO;
import com.linkage.lcims.base.po.broadband.BBroadbandUser;
import com.linkage.lcims.base.po.broadband.BBroadbandUserPK;
import com.linkage.util.config.BusinessConfig;
import com.linkage.util.exception.DAOException;
import com.linkage.util.service.Hash163;

/**
 * @author Administrator
 * 
 */
public class BroadbandUserDAOImpl extends HibernateBaseDAOImpl implements
		BroadbandUserDAO {
	private static final String PO_CLASS = "com.linkage.lcims.base.po.broadband.BBroadbandUser";
	private static final String PK_CLASS = "com.linkage.lcims.base.po.broadband.BBroadbandUserPK";

	/**
	 * 
	 */
	public BroadbandUserDAOImpl() {
		super();
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.linkage.lcims.base.dao.broadband.interfaces.BroadbandUserDAO#getBroadbandUser(java.lang.String,
	 *      java.lang.String)
	 */
	public BBroadbandUser getBroadbandUser(String userName, int serviceType)
			throws DAOException {
		try {
			String tableName = Hash163.BRDHash(userName);
			BBroadbandUserPK pk = new BBroadbandUserPK(userName, new Integer(
					serviceType));
			BBroadbandUser user = (BBroadbandUser) this.getByID(tableName, pk);
			return user;
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.linkage.lcims.base.dao.broadband.interfaces.BroadbandUserDAO#saveBroadbandUser(com.linkage.lcims.base.po.broadband.BBroadbandUser,
	 *      java.lang.String)
	 */
	public void saveBroadbandUser(BBroadbandUser user) throws DAOException {
		try {
			String tableName = Hash163.BRDHash(user.getComp_id().getUsername());
			this.save(tableName, user);
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.linkage.lcims.base.dao.broadband.interfaces.BroadbandUserDAO#updateBroadbandUser(com.linkage.lcims.base.po.broadband.BBroadbandUser,
	 *      int, java.lang.String)
	 */
	public void updateBroadbandUser(BBroadbandUser user) throws DAOException {
		try {
			String tableName = Hash163.BRDHash(user.getComp_id().getUsername());
			this.merge(tableName, user);
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}

	}

	public void updateFields(Map<String,Object> keys, Map<String,Object> fieldValues)
			throws DAOException {
		try {
			String tableName=Hash163.BRDHash((String)keys.get("username"));
			this.updateField(tableName, PO_CLASS, PK_CLASS, keys, fieldValues);
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}

	public void deleteBroadbandUser(BBroadbandUser user) throws DAOException {
		try {
			String tableName = Hash163.BRDHash(user.getComp_id().getUsername());
			this.delete(tableName, user.getComp_id());
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.linkage.lcims.base.dao.broadband.interfaces.BroadbandUserDAO#updateBroadbandUserByHql(java.lang.String,
	 *      java.lang.String[], java.lang.Object[])
	 */
	public void updateBroadbandUserByHql(String hql, String[] paramNames,
			Object[] values) throws DAOException {
		this.update(hql, paramNames, values);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.linkage.lcims.base.dao.broadband.interfaces.BroadbandUserDAO#getBroadbandUserForBatBind(java.lang.String,
	 *      java.lang.String, java.lang.String, java.lang.String,
	 *      java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public int getBroadbandUserForBatBind(String username, String servicetype,
			String areano, boolean areanoflag, String ori_nasIP)
			throws DAOException {

		try {
			String sql = "";
			String sqltemp = "";
			sql += "select count(*) from B_BROADBAND_USER";
			String sqlwhere = " where";
			sqlwhere += " servicetype=" + servicetype;

			if (!(username == null || username.trim().equals(""))) {
				sqlwhere += " and username like '" + username + "%'";
			}
			if (areanoflag == false) {
				sqlwhere += " and areano='" + areano + "'";
			} else {
				sqlwhere += " and areano like '" + areano + "%'";
			}

			if (!(ori_nasIP == null || ori_nasIP.trim().equals("") || ori_nasIP
					.equals("0.0.0.0"))) {
				sqlwhere += " and nasip='" + ori_nasIP + "'";
			}

			int tableCount = Integer.parseInt(BusinessConfig
					.getCommonTableCount());
			List list = null;
			int total = 0;
			for (int i = 0; i < tableCount; i++) {
				sqltemp = (i < 10 ? ("0" + i) : String.valueOf(i)) + sqlwhere;
				list = this.query(sql + sqltemp);
				// System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAAa"+ sql +
				// sqltemp);

				// System.out.println("list.size"+ list.size());
				// System.out.println("list.get"+ list.get(0).toString());
				if (list != null) {
					total += Integer.parseInt(list.get(0).toString().trim());
					// System.out.println("SSSSSSSSSSSSSSSSSSSSSS"+ total);
				}
			}

			return total;
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.linkage.lcims.base.dao.broadband.interfaces.BroadbandUserDAO#updateBroadbandUserForBatBind(java.lang.String,
	 *      java.lang.String, java.lang.String, java.lang.String,
	 *      java.lang.String, java.lang.String, java.lang.String)
	 */
	public int updateBroadbandUserForBatBind(String username,
			String servicetype, String areano, boolean areanoflag,
			String ori_nasIP, String bindtype, String bindattr)
			throws DAOException {
		try {
			String sqltemp = "";
			String updatesql = "update B_BROADBAND_USER";
			String setsql = " set bindtype=" + bindtype + ", bindattr='"
					+ bindattr + "'";
			String sqlwhere = " where";
			sqlwhere += " servicetype=" + servicetype;

			if (!(username == null || username.trim().equals(""))) {
				sqlwhere += " and username like '" + username + "%'";
			}
			if (areanoflag == false) {
				sqlwhere += " and areano='" + areano + "'";
			} else {
				sqlwhere += " and areano like '" + areano + "%'";
			}
			if (!(ori_nasIP == null || ori_nasIP.trim().equals("") || ori_nasIP
					.equals("0.0.0.0"))) {
				sqlwhere += " and nasip='" + ori_nasIP + "'";
			}
			int result = 0;
			int tableCount = Integer.parseInt(BusinessConfig
					.getCommonTableCount());
			for (int i = 0; i < tableCount; i++) {
				sqltemp = (i < 10 ? ("0" + i) : String.valueOf(i)) + setsql
						+ sqlwhere;
				// System.out.println("AAAAAAAAAAAAAAAAAAAAAAAAAAAA"+ updatesql
				// + sqltemp);
				result += this.update(updatesql + sqltemp);
			}
			return result;
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}

	public void updateBroadbandUserForGreenUser(BBroadbandUser user,
			int preServicetype) throws DAOException {
		try {
			String tableName = Hash163.BRDHash(user.getComp_id().getUsername());
			String hqlforupdate = "update " + tableName
					+ " set optionalservice ='1' where username ='"
					+ user.getComp_id().getUsername() + "' and servicetype ="
					+ preServicetype;
			this.saveBroadbandUser(user);
			this.updateBySql(hqlforupdate);
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}

	public void cancelBroadbandUserForGreenUser(
			BBroadbandUser user, int preServicetype) throws DAOException {
		try {
			String tableName = Hash163.BRDHash(user.getComp_id().getUsername());
			String hqlfordelete = "delete " + tableName + " where username ='"
					+ user.getComp_id().getUsername() + "' and servicetype ="
					+ 204;
			String hqlforupdate = "update " + tableName
					+ " set optionalservice = NULL where username ='"
					+ user.getComp_id().getUsername() + "' and servicetype ="
					+ preServicetype;
			this.deleteBySql(hqlfordelete);
			this.updateBySql(hqlforupdate);
		} catch (Exception e) {
			throw new DAOException(e, this.getClass());
		}
	}

}
